<template>
  <div class="pager-main" v-if="total != 0">
    <div @click="pre" class="pre-btn">上一页</div>
    <div class="pageIndex">{{ currPage }}</div>
    <div @click="next" class="next-btn">下一页</div>
    <div>
      共{{ totalPage }}页，到第<input
        type="number"
        @change="handleInput"
        v-model="pageIndex"
      />
      页
    </div>
    <!-- <div class="btn" @click="submit">确定</div> -->
  </div>
</template>
<script>
export default {
  name: "pager",
  props: {
    currPage: {
      type: Number,
      default: () => 1,
    },
    total: {
      type: Number,
      default: () => 1,
    },
    size: {
      type: Number,
      default: () => 10,
    },
  },
  data() {
    return {
      pageIndex: "",
      totalPage: 0,
    };
  },
  created() {
    //总页数
    let page = parseInt(this.total / this.size);
    page = this.total - page * this.size > 0 ? page + 1 : page;
    this.totalPage = page;
  },
  methods: {
    pre() {
      // this.currPage = this.currPage - 1 < 0 ? 1 : this.currPage - 1;
      let curr = this.currPage - 1 <= 0 ? 1 : this.currPage - 1;
      if (curr != this.currPage) {
        this.submit(curr);
      }
    },
    next() {
      let curr =
        this.currPage + 1 > this.totalPage ? this.totalPage : this.currPage + 1;
      // console.log(page, curr);
      if (curr != this.currPage) {
        this.submit(curr);
      }
    },
    handleInput() {
      let index = parseInt(this.pageIndex);
      this.pageIndex =
        index <= 0
          ? 1
          : index > parseInt(this.totalPage)
          ? this.totalPage
          : index;
      if (this.pageIndex != this.currPage) {
        this.submit(this.pageIndex);
      }
    },
    submit(curr) {
      this.$emit("pageEvent", curr);
    },
  },
};
</script>
<style lang="less" scoped>
.pager-main {
  display: flex;
  justify-content: center;
  padding: 50px 0;
  align-items: center;
  box-sizing: border-box;
  .pre-btn,
  .pageIndex,
  .next-btn {
    border: 1px solid #d8d8d8;
    border-radius: 4px;
    padding: 6px 10px;
    line-height: 1;
    box-sizing: border-box;
    margin-right: 5px;
  }
  .btn {
    width: 60px;
    height: 28px;
    line-height: 28px;
    background: #cb3e3e;
    border-radius: 4px;
    text-align: center;
    color: #fff;
    margin-left: 20px;
    cursor: pointer;
  }
}
input {
  width: 61px;
  border: 1px solid #d8d8d8;
  border-radius: 4px;
  outline: none;
  padding: 0 5px;
  margin: 0 5px;
  text-align: center;
}
</style>